package com.orphan.marketBack.service.impl;

import com.orphan.marketBack.dao.OrderGoodsMapper;
import com.orphan.marketBack.dao.OrderMapper;
import com.orphan.marketBack.po.Order;
import com.orphan.marketBack.po.OrderGoods;
import com.orphan.marketBack.service.OrderService;
import com.orphan.util.dto.OrderDto;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Override
    public List<Order> get() {
        return null;
    }


    @RabbitListener(queues = "newOrderQueue")
    public void get(OrderDto orderDto){
        Order order = new Order();
        order.setOrderId(orderDto.getOrderId());
        order.setUserId(orderDto.getUserId());
        order.setTotalAmount(orderDto.getTotalAmount());
        order.setConsignee(orderDto.getConsignee());
        order.setConsigneeTel(orderDto.getConsigneeTel());
        order.setOrderAddress(orderDto.getOrderAddress());
        order.setOrderTime(orderDto.getOrderTime());
        order.setPayMethod(orderDto.getPayMethod());
        order.setOrderStatus(orderDto.getOrderStatus());
        order.setPayStatus(orderDto.getPayStatus());
        orderMapper.insert(order);

        OrderGoods goods = new OrderGoods();
        for (int i = 0; i < orderDto.getGoodsList().size(); i++) {
            goods.setOrderId(orderDto.getGoodsList().get(i).getOrderId());
            goods.setGoodsType(orderDto.getGoodsList().get(i).getGoodsType());
            goods.setGoodsName(orderDto.getGoodsList().get(i).getGoodsName());
            goods.setGoodsPrice(orderDto.getGoodsList().get(i).getGoodsPrice());
            goods.setGoodsCount(orderDto.getGoodsList().get(i).getGoodsCount());
            goods.setGoodsOrderStatus(orderDto.getGoodsList().get(i).getGoodsOrderStatus());
            goods.setGoodsPicture(orderDto.getGoodsList().get(i).getGoodsPicture());
        }
        orderGoodsMapper.insert(goods);

        System.out.println("orderDto = " + orderDto);
    }

}
